import TencentMap from './lib/qqmap-wx-jssdk.min' // 腾讯地图的sdk
const mapKey = 'SHOBZ-V45KJ-5C2FK-KUP7A-ALPGT-GQB3F' // 申请下来的开发者密钥key(腾讯地图)

// 根据经纬度获取当前城市信息
export const getLocalCity = (longitude, latitude, that) => {
  let promise
  // #ifndef H5
  promise = new Promise((resolve, reject) => {
    const map = new TencentMap({
      key: mapKey
    })
    // 通过这个方法来实现经纬度反推省市区
    map.reverseGeocoder({
      location: {
        longitude: longitude,
        latitude: latitude
      },
      success: res => {
        resolve(res.result)
      },
      fail: err => {
        reject(err)
      }
    })
  })
  // #endif

  // #ifdef H5
  promise = new Promise((resolve, reject) => {
    const locationObj = latitude + ',' + longitude
    const url = 'https://apis.map.qq.com/ws/geocoder/v1/'
    that
      .$jsonp(url, {
        key: mapKey,
        location: locationObj,
        output: 'jsonp',
        callbackName: 'getJsonData'
      })
      .then(res => {
        resolve(res.result)
      })
      .catch(err => {
        reject(err)
      })
  })
  // #endif
  return promise
}
